"""
@Author: li
@Email: lijianqiao2906@live.com
@FileName: permission.py
@DateTime: 2025/07/06
@Docs: 权限管理API
"""

from fastapi import APIRouter, Depends

from app.schemas.base import PaginatedResponse
from app.schemas.permission import PermissionRead
from app.services.system import permission_service
from app.utils.deps import PaginationDep, PermissionChecker

router = APIRouter()


@router.get(
    "/",
    response_model=PaginatedResponse[PermissionRead],
    summary="获取权限列表",
    dependencies=[Depends(PermissionChecker("permission:read"))],
)
async def get_all_permissions(
    pagination: PaginationDep,
) -> PaginatedResponse[PermissionRead]:
    """
    获取所有权限，支持分页。
    权限由系统自动生成，此接口只读。
    """
    permissions, total = await permission_service.get_active_paginated(
        order_by=["module", "sort_order", "id"], **pagination
    )
    return PaginatedResponse(
        total=total,
        page=pagination["page"],
        page_size=pagination["page_size"],
        data=[PermissionRead.model_validate(p) for p in permissions],
    )
